﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Linq;
using TI_BAL;

namespace SecuritySystem
{
    public partial class AddEditAccounts : Form
    {
        TIEntities dbContext;

        public AddEditAccounts()
        {
            InitializeComponent();
            dbContext = new TIEntities();
            LoadAccounts();
            comboBoxAccountName.SelectedIndex = 0;
        }
        private void LoadAccounts()
        {
            List<Account> accounts = (from ac in dbContext.Accounts select ac).ToList();
            comboBoxAccountName.DataSource = null;
            comboBoxAccountName.DataSource = accounts;
            comboBoxAccountName.DisplayMember = "Account_Name";
            comboBoxAccountName.ValueMember = "Account_ID";

           
            
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            string accountName =comboBoxAccountName.Text.Trim();
            if (!string.IsNullOrEmpty(accountName))
            {
                Account account = (from ac in dbContext.Accounts where ac.Account_Name == accountName select ac).FirstOrDefault();
                if (account != null)
                {
                    account.Account_Name = accountName;
                    account.Account_Description = textBoxAccountDesc.Text.Trim();
                    
                   
                }
                else
                {
                    account = new Account();
                    account.Account_Name = accountName;
                    account.Account_Description = textBoxAccountDesc.Text.Trim();
                    account.DateCreated = DateTime.Now;
                    dbContext.AddToAccounts(account);
                }
                dbContext.SaveChanges();
                MessageBox.Show("Changes Saved Successfully");
            }
        }

        private void comboBoxAccountName_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxAccountName.SelectedIndex > -1)
            {
                string accountName = comboBoxAccountName.Text.Trim();
                Account account = (from ac in dbContext.Accounts where ac.Account_Name == accountName select ac).FirstOrDefault();
                if (account != null)
                {
                    textBoxAccountDesc.Text = account.Account_Description;
                  
                }
            }
        }

        private void AddEditAccounts_Load(object sender, EventArgs e)
        {

        }
    }
}